package com.nl.controller.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.MgrType;
import com.nl.controller.web.bean.SmsCounty;
import com.nl.controller.web.bean.SmsMgrInfo;
import com.nl.controller.web.bean.SmsMgrTemplateInfo;
import com.nl.controller.web.bean.SmsReportDetail;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.service.SmsBusiService;
import com.nl.controller.web.service.SmsMgrService;
import com.nl.controller.web.service.SmsReportDetailService;
import com.nl.controller.web.service.SmsUserService;
import com.nl.controller.web.service.TestService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/util")
public class UtilController {
	@Autowired
	private SmsReportDetailService smsReportDetailService;
	@Autowired
	private SmsUserService smsUserService;
	@Autowired
	private SmsBusiService  smsBusiService;
	@Autowired
	private SmsMgrService smsMgrService;
	
	@Autowired
	private TestService testService;
	

	@ApiOperation(value = "身份识别", notes = "身份识别")
	@PostMapping("findUser")
	public SmsUsers findUser(HttpSession session) {
		return (SmsUsers) session.getAttribute("user");
	}

	@ApiOperation(value = "短信发送明细查询", notes = "查询区域下的基站")
	@PostMapping("findStationInfo")
	private Response findStationInfo(@RequestParam(value = "regionId") String regionId, @RequestParam(value = "cityId") String cityd, @RequestParam(value = "mgrId") String mgrId) {
		List<SmsReportDetail> list = smsReportDetailService.findStationInfo(regionId, cityd, mgrId);
		log.info("查询区域下的基站：{}", mgrId);
		log.debug("查询区域下的基站返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "短信发送明细查询", notes = "查询项目下的区域")
	@PostMapping("findRegionInfo")
	private Response findRegionInfo(@RequestParam(value = "mgrId") String mgrId,
			@RequestParam(value = "cityId") String cityId) {
		List<SmsReportDetail> list = smsReportDetailService.findRegionInfo(mgrId, cityId);
		log.info("查询项目下的区域：{}", mgrId);
		log.debug("查询项目下的区域返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "用户管理模板", notes = "根据城市查询项目信息")
	@ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
	@PostMapping("findByCityId")
	public Response<SmsMgrInfo> findOperatorByCityId(
			@RequestParam(value = "cityId") String cityId,HttpSession session
			) {
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String duty = user.getDuty();
		String msisdn = user.getMsisdn();
		log.info("根据城市查询项目信息参数：{}", cityId);
		List<SmsMgrInfo> list = new ArrayList<>();
		if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
			list = smsUserService.findMgrByCityId(cityId,duty,msisdn);
		}
		log.debug("根据城市查询项目信息返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}


	@ApiOperation(value = "项目维护管理", notes = "根据区县查询项目")
	@ApiImplicitParam(name = "countyId", value = "区县编号", required = true, dataType = "String")
	@PostMapping("findByMgrId")
	public Response<SmsMgrInfo> findByMgrId(
			@RequestParam(value = "countyId") String countyId,HttpSession session) {
		log.info("根据区县查询项目参数：{}", countyId);
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String duty = user.getDuty();
		String msisdn = user.getMsisdn();
		List<SmsMgrInfo> list = new ArrayList<SmsMgrInfo>();
		if (countyId != null && !"undefined".equals(countyId) && countyId != "") {
			if(countyId.length()>2){
				list = smsUserService.findMgrByCountyId(countyId,duty,msisdn);
			}else if(countyId.length()==2){
				list = smsUserService.findMgrByCityId(countyId,duty,msisdn);
			}
		}
		log.debug("根据城市查询项目信息返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "短信项目管理", notes = "根据城市查询扩展码信息")
	@ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
	@PostMapping("findExdId")
	public Response<String> findExtIdByCityId(
			@RequestParam(value = "cityId") String cityId
			) {
		log.info("根据城市查询扩展码信息参数：{}", cityId);
		List<String> list = new ArrayList<>();
		if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
			list = smsUserService.findExtIdByCityId(cityId);
		}
		log.debug("根据城市查询扩展码信息返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "短信项目管理", notes = "项目名称联想")
	@PostMapping("selectMgrNameLike")
	public Response<String> selectMgrNameLike(@RequestParam(value = "key", required = true) String key,
			@RequestParam(value = "cityId", required = false) String cityId,
			@RequestParam(value = "countyId", required = false) String countyId
			) {

		log.info("项目名称联想参数：{}", key);
		List<SmsMgrInfo> list = new ArrayList<>();
		if (key != null && !"undefined".equals(key) && key != "") {
			list = smsUserService.selectMgrNameLike(key,cityId,countyId);
		}
		log.debug("项目名称联想返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}     
	
	@ApiOperation(value = "短信项目管理" , notes = "项目类型查询")
	@PostMapping("findMgrType")
	public  Response<List<MgrType>> findMgrType(){		
		log.info("查询项目类型列表");
		List<MgrType> list = smsMgrService.findMgrType();
		log.debug("查询项目类型列表长度："+list.size());
		return ResponseUtil.sucess(list);
	}
	
	@ApiOperation(value = "短信项目管理" , notes = "项目类型查询")
	@PostMapping("findMgrTypeById")
	public  Response<String> findMgrTypeById(@RequestParam(value = "mgrType", required = false) String typeId){		
		log.info("查询项目类型名称");
		log.info("----------"+typeId);

		String typeName = smsMgrService.findMgrTypeById(typeId);
		//log.debug("查询项目类型列表长度："+list.size());
		//log.info("----------"+typeName);
		return ResponseUtil.sucess(typeName);
	}
	

	@ApiOperation(value = "项目维护管理", notes = "根据城市查询区县信息")
	@ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
	@PostMapping("findCountyByCityId")
	public Response<SmsCounty> findCountyByCityId(@RequestParam(value = "cityId") String cityId) {
		log.info("根据城市查询区县信息参数：{}", cityId);
		List<SmsCounty> list = new ArrayList<SmsCounty>();
		if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
			list = smsUserService.findCountyByCityId(cityId);
		}
		log.debug("根据城市查询区县信息返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "项目维护管理", notes = "根据区县查询项目")
	@ApiImplicitParam(name = "countyId", value = "区县编号", required = true, dataType = "String")
	@PostMapping("findByMgrName")
	public Response<SmsMgrTemplateInfo> findByMgrName(
			@RequestParam(value = "groupId",required = false) String groupId,
			@RequestParam(value = "countyId" ,required = false) String countyId,
			@RequestParam(value = "mgrName",required = false) String mgrName,HttpSession session) {
		log.info("根据区县查询项目参数：{}", countyId);
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String duty = user.getDuty();
		String msisdn = user.getMsisdn();
		List<SmsMgrTemplateInfo> list = new ArrayList<SmsMgrTemplateInfo>();
		if (countyId != null && !"undefined".equals(countyId) && countyId != "") {
			if(countyId.length()>2){
				list = smsUserService.findMgrByCountyId(countyId,duty,msisdn,mgrName,groupId);
			}else if(countyId.length()==2){
				list = smsUserService.findMgrByCityId(countyId,duty,msisdn,mgrName,groupId);
			}
			System.out.println(list.toString());
			if(CollectionUtils.isEmpty(list)){
				list = new ArrayList<>();
			}
		}
		log.debug("根据城市查询项目信息返回数据：{}", list.size());
		return ResponseUtil.sucess(list);
	}

	@ApiOperation(value = "获取项目名称是否存在", notes = "获取项目名称是否存在")
	@PostMapping("getMgrName")
	public Response<Integer> getMgrName(@RequestParam(value = "mgrName") String mgrName) {
		int flag = smsUserService.getMgrName(mgrName.trim());
		log.debug("获取项目名称是否存在：{}", flag);
		return ResponseUtil.sucess(flag);
	}
	@ApiOperation(value = "获取项目管理项目名称", notes = "获取项目管理项目名称是否存在")
	@PostMapping("getUserMgrName")
	public Response<SmsMgrInfo> findByMgrId(HttpSession session) {
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String duty = user.getDuty();
		List<SmsMgrInfo> list = new ArrayList<SmsMgrInfo>();
		if("3".equals(duty)){
			String[] mgrIds = user.getMgrId().split(",");
			for (String mgrId : mgrIds) {
				SmsMgrInfo smsMgrInfo = smsUserService.findByMgrId(mgrId);
				list.add(smsMgrInfo);
			}
		}
		return ResponseUtil.sucess(list);    
	}

	@ApiOperation(value = "根据项目名查询项目", notes = "根据项目名查询项目")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mgrName", value = "项目简称", required = true, dataType = "String"),
	})
	@PostMapping("findMgrByName")
	private @ResponseBody
	Response findMgrByName(
			@RequestParam("mgrName") String mgrName
			) {
		log.info(" 根据项目名查询项目：{}", mgrName);
		SmsMgrInfo smsMgrInfo=smsBusiService.findMgrByName(mgrName);
		log.debug("根据项目名查询项目：{}", smsMgrInfo.toString());
		return ResponseUtil.sucess(smsMgrInfo);
	}

	@PostMapping("contentCount")
	private @ResponseBody
	Response qryContentCount(
			@RequestParam("content") String content
			) {
		log.info(" 查询发送短信的条数：{}", content);
		byte[] messageUCS2=null;
		try {
			messageUCS2 = content.getBytes("UnicodeBigUnmarked");
		} catch (UnsupportedEncodingException e) {
			log.info("计算内容长度异常");
			e.printStackTrace();
			return ResponseUtil.error(-1, "计算内容长度异常");
		}
		int messageUCS2Len = messageUCS2.length;// 短信长度
		//短短信
		if(messageUCS2.length<=140){
			return ResponseUtil.sucess(1);
			//长短信
		}else{
			int messageUCS2Count = messageUCS2Len / (140 - 6) + 1;
			if(messageUCS2Len % (140 - 6)==0){
				messageUCS2Count = messageUCS2Len / (140 - 6);
			}else{
				messageUCS2Count = messageUCS2Len / (140 - 6) + 1;
			}
			return ResponseUtil.sucess(messageUCS2Count);
		}      
	}
	
	@RequestMapping("testRedsi")
	private @ResponseBody
	Response testRedis(
			) {
		Map<String , String > result = testService.testRedis();  
		return ResponseUtil.sucess(result);
	}
}
